<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="teacher/teacher-common::commonCss(~{::title},~{},~{::script},~{::style})">
    <title>JAVA在线考试-试卷管理</title>
    <script th:src="@{/layui/xm-select.js}"></script>
    <script th:src="@{/laydate/laydate.js}"></script>
    <style>
        .body{
            background-color: #f8f8f8;
        }
    </style>

    <script type="text/html" id="toolbar">
        <div class="layui-btn-container">
            <button lay-event="deletes" class="layui-btn layui-btn-danger">删除选中行</button>
            <button lay-event="save" class="layui-btn layui-btn-normal">新增</button>
        </div>
    </script>
    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a><br/>
    </script>
    <script th:inline="javascript">
        layui.use(['table','element','laydate','form'], function(){
            var table = layui.table;
            var element = layui.element;
            var laydate = layui.laydate;
            var form = layui.form;

            var repoIds = xmSelect.render({
                el: '#repoIds'
            })

            laydate.render({
                elem: '#search-createTime'
                ,range: true
            });

            $("#searchBtn").click(function () {
                reload();
            });
            $("#resetBtn").click(function () {
                $("#searchpaperInfoForm")[0].reset();
                tableIns.reload({
                    where: {
                        "msg": "",
                        "code": "",
                        "data": {
                            "title": '',
                            "startTime": '',
                            "endTime": ''
                        }
                    }
                    , page: {
                        curr: 1
                    }
                });
            });

            var tableIns = table.render({
                elem: '#paperTable'
                , url: '/paper/listForPage'
                , toolbar: '#toolbar'
                , contentType: 'application/json'
                , method: 'post'
                , where: {
                    "msg": "",
                    "code": "",
                    "data": {
                        "title": $('#search-title').val(),
                        "startTime": $("#search-createTime").val().split(" - ")[0],
                        "endTime": $("#search-createTime").val().split(" - ")[1] != null ? $("#search-createTime").val().split(" - ")[1] : ""
                    }
                }
                , defaultToolbar: []
                , page: true
                , limits: [5, 10, 20]
                , limit: 10
                , cols: [
                    [
                        {type: 'checkbox', fixed: 'left', rowspan:2}
                        , {field: 'id', title: 'ID', hide: true, rowspan:2}
                        , {field: 'title', title: '试卷名', rowspan:2}
                        , {title: '单选题', colspan:2, align:'center'}
                        , {title: '多选题', colspan:2, align:'center'}
                        , {title: '判断题', colspan:2, align:'center'}
                        , {field: 'score', title: '总分值', rowspan:2}
                        , {field: 'createTime', title: '创建时间', rowspan:2}
                        , {title: '操作', fixed: 'right', width: 70, toolbar: '#bar', rowspan:2}
                    ]
                        ,
                    [
                        {field: 'radioCount', title: '数量', align:'center'}
                        , {field: 'radioScore', title: '分值', align:'center'}
                        , {field: 'selectCount', title: '数量', align:'center'}
                        , {field: 'selectScore', title: '分值', align:'center'}
                        , {field: 'judgeCount', title: '数量', align:'center'}
                        , {field: 'judgeScore', title: '分值', align:'center'}
                    ]
                ]
                ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.result.code, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.records //解析数据列表
                    };
                }
            });

            //监听工具条
            table.on('tool(paperTable)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case 'delete':
                        layer.confirm('真的删除么', function (index) {
                            $.ajax({
                                url : '/paper/delete/'+data.id,
                                type : 'post',
                                dataType : 'json',
                                success : function(n) {
                                    if (n.result.status) {
                                        reload();
                                    }else {
                                        layer.msg("删除试卷失败,"+n.result.msg);
                                    }
                                },
                                error: function(n) {
                                    layer.msg("删除试卷失败,"+n.responseJSON.result.msg);
                                }
                            });
                            layer.close(index);
                        });
                        break;
                };
            });
            table.on('toolbar(paperTable)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'deletes':
                        var data = checkStatus.data;
                        var size = data.length;
                        console.log(data);
                        if (size != 0) {
                            layer.confirm('真的删除么', function (index) {
                                for (let i = 0; i < size; i++) {
                                    $.ajax({
                                        url : '/paper/delete/'+data[i].id,
                                        type : 'post',
                                        dataType : 'json',
                                        success : function(n) {
                                            if (n.result.status) {
                                                reload();
                                            }else {
                                                layer.msg("删除试卷失败,"+n.result.msg);
                                            }
                                        },
                                        error: function(n) {
                                            layer.msg("删除试卷失败,"+n.responseJSON.result.msg);
                                        }
                                    });
                                }
                                layer.close(index);
                                reload();
                            });
                        } else {
                            layer.msg("请勾选要删除的试卷");
                        }
                        break;
                    case 'save':
                        layer.open({
                            type: 1,
                            area: ['700px', '500px'],
                            shadeClose: true, //点击遮罩关闭
                            title: '添加试卷',
                            btn: ['确定', '取消'],
                            content: $('#paperInfoFrame'),
                            success: function (index, layero) {
                                $("#paperInfoForm")[0].reset();
                                $.ajax({
                                    url: '/repo/list',
                                    type:"get",
                                    dataType:"json",
                                    success:function (data){
                                        if (data.result.status) {
                                            var text = '[' ;
                                            $.each(data.data,function (i,n){
                                                text +='{ "name":"'+n.title+'" , "value":"'+n.id+'" },';
                                            });
                                            text = text.substr(0, text.length - 1);
                                            text += ']' ;
                                            repoIds.update({
                                                data: JSON.parse(text)
                                            })
                                        }else {
                                            layer.msg("查询题库列表失败,"+n.result.msg);
                                        }
                                        form.render(null,"paperInfoForm");
                                    }
                                });
                                layui.form.render();
                            },
                            yes: function (index, layero) {
                                var title = $("#title").val();
                                var radioCount  = $("#radioCount ").val();
                                var radioScore  = $("#radioScore ").val();
                                var selectCount = $("#selectCount").val();
                                var selectScore = $("#selectScore").val();
                                var judgeCount  = $("#judgeCount ").val();
                                var judgeScore  = $("#judgeScore ").val();
                                var repos = repoIds.getValue('value');

                                var score = radioCount*radioScore + selectCount*selectScore + judgeCount*judgeScore;
                                if(score <= 0){
                                    layer.msg("总分不能为零或负数",{icon:'5'});
                                    return;
                                }
                                if(repos.length<=0){
                                    layer.msg("至少要选择一个题库",{icon:'5'});
                                    return;
                                }

                                $.ajax({
                                    url : '/paper/save',
                                    type : 'post',
                                    contentType : 'application/json',
                                    dataType: 'json',
                                    data: JSON.stringify({
                                        "msg":"",
                                        "code":"",
                                        "data": {
                                            "title": title,
                                            "radioCount": radioCount ,
                                            "radioScore": radioScore ,
                                            "selectCount": selectCount,
                                            "selectScore": selectScore,
                                            "judgeCount": judgeCount ,
                                            "judgeScore": judgeScore ,
                                            "repoIds": repos
                                        }
                                    }),
                                    success : function(n) {
                                        if (n.result.status) {
                                            layer.msg("添加试卷成功");
                                            layer.close(index);
                                            reload();
                                        }else {
                                            layer.msg("添加试卷失败,"+n.result.msg);
                                        }
                                    },
                                    error: function(n) {
                                        layer.msg("添加试卷失败,"+n.responseJSON.result.msg);
                                    }
                                });
                            },
                            end: function (res) {
                                $("#paperInfoFrame").css("display", 'none');
                            }
                        });
                        break;
                }
                ;
            });

            function reload() {
                tableIns.reload({
                    where: {
                        "msg": "",
                        "code": "",
                        "data": {
                            "title": $('#search-title').val(),
                            "startTime": $("#search-createTime").val().split(" - ")[0],
                            "endTime": $("#search-createTime").val().split(" - ")[1] != null ? $("#search-createTime").val().split(" - ")[1] : ""
                        }
                    }
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }

        });

    </script>
</head>
<body>
<div class="layui-layout layui-layout-admin">

    <div th:replace="teacher/teacher-common::header"></div>
    <div th:replace="teacher/teacher-common::side('paper')"></div>

    <div class="layui-body body">
        <div class="layui-fluid" style="padding: 15px;">
            <div class="layui-card">
                <form class="layui-form layui-card-header" style="padding: 15px" lay-filter="searchpaperInfoForm" id="searchpaperInfoForm">
                    <div class="layui-form-item">
                        <div class="layui-inline" style="width:150px;">
                            <input class="layui-input" id="search-title" autocomplete="off" placeholder="试卷名">
                        </div>
                        <div class="layui-inline">
                            <input type="text" id="search-createTime" placeholder="创建时间" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline" style="float: right;">
                            <button type="button" class="layui-btn" id="searchBtn">
                                <i class="layui-icon layui-icon-search"></i> 搜索
                            </button>
                            <button  type="button" class="layui-btn" id="resetBtn">重置</button>
                        </div>

                    </div>
                </form>

                <div class="layui-card-body">
                    <div style="padding-bottom:10px;">
                        <table class="layui-hide" id="paperTable" lay-skin="line" lay-filter="paperTable">
                        </table>
                    </div>
                </div>

            </div>
        </div>

    </div>

    <div th:replace="teacher/teacher-common::footer"></div>
</div>

</body>
<div id="paperInfoFrame" style="display:none;">
    <form class="layui-form" lay-filter="paperInfoForm" id="paperInfoForm" style="padding-right:30px">
        <div class="layui-form-item" style="margin-top: 30px">
            <label class="layui-form-label">试卷名</label>
            <div class="layui-input-block">
                <input id="title" type="text" placeholder="请输入试卷名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">单选题</label>
            <div class="layui-inline" style="margin-right: 0;">
                <input class="layui-input" id="radioCount" autocomplete="off" placeholder="数量">
            </div>
            <div class="layui-inline" style="margin-right: 0;">
                <input class="layui-input" id="radioScore" autocomplete="off" placeholder="分值">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">多选题</label>
            <div class="layui-inline" style="margin-right: 0;">
                <input class="layui-input" id="selectCount" autocomplete="off" placeholder="数量">
            </div>
            <div class="layui-inline" style="margin-right: 0;">
                <input class="layui-input" id="selectScore" autocomplete="off" placeholder="分值">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">判断题</label>
            <div class="layui-inline" style="margin-right: 0;">
                <input class="layui-input" id="judgeCount" autocomplete="off" placeholder="数量">
            </div>
            <div class="layui-inline" style="margin-right: 0;">
                <input class="layui-input" id="judgeScore" autocomplete="off" placeholder="分值">
            </div>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label">选择题库</label>
            <div class="layui-input-block" >
                    <div id="repoIds" class="xm-select-demo"></div>
            </div>
        </div>
    </form>
</div>
</html>